﻿<%@ WebHandler Language="C#" Class="GetProductHandler" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Text;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using ITPCTaxFormModel;

public class GetProductHandler : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        using (var entity_context = new ITPCTaxFormEntities())
        {
            int limit = 0;
            if (HttpContext.Current.Request.QueryString["limit"] != null)
                limit = Convert.ToInt32(HttpContext.Current.Request.QueryString["limit"]);

            string q = "";
            if (HttpContext.Current.Request.QueryString["q"] != null)
                q = HttpContext.Current.Request.QueryString["q"];

            var _mtable = (List<Product>)context.Session["_mtable"];
            var sb = new StringBuilder();
            if (q.Trim() != "")
            {
                List<Product> display = null;
                display = _mtable.Where(p => p.SearchString.ToLower().Contains(q)).OrderBy(p => p.SearchString).ToList();
                if (limit > 0)
                    display.Take(limit).ToList();
                else
                    display.ToList();

                if (display.Count() > 0)
                {
                    //Display on UserInput TextBox
                    foreach (var r in display)
                        sb.AppendFormat("{0}{1}|{2}|{3}|{4}|{5}", (sb.Length > 0 ? "\n" : ""), r.SearchString, r.ProductID, r.ProductCategoryID, r.ProductCode, r.ProductCategoryCode);
                
                     // Define Response type = text
                    context.Response.ContentType = "text/plain";
                    context.Response.Write(sb.ToString());
                
                }
                else
                    sb=null;
                
               
            }
        }
    }
    
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

